constructive algorithms dp math number theory *1900

Please click on ads to support us..

Python Code:

def getAllFactors(x):     factors=[]
    for i in range(1,int(x**0.5)+1):
        if x%i==0:
            factors.append(i)
            if x//i!=i:
                factors.append(x//i)
    return sorted(factors)

def main():
    
    t = int(input())
    allans = []
    for _ in range(t):
        x, d = readIntArr()
        nSplit = 0
                while x % d == 0:
            x //= d
            nSplit += 1
        if nSplit == 1:
            ans = 'no'
        else:
                                    ans = 'no'
            xdivisors = getAllFactors(x)
            ddivisors = getAllFactors(d)
            
            if 1 in xdivisors: xdivisors.remove(1);
            if x in xdivisors: xdivisors.remove(x)
            if 1 in ddivisors: ddivisors.remove(1);
            if d in ddivisors: ddivisors.remove(d)
            
            if len(xdivisors) > 0:                 ans = 'yes'
            
            elif nSplit == 3:
                                for ddiv in ddivisors:
                    if (ddiv * x) % d != 0:
                        ans = 'yes'
            elif nSplit > 3:
                                if len(ddivisors) > 0: 
                    ans = 'yes'
            
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return
 
import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <map>
#include <numeric>
#include <time.h>
#include <numeric>
#include <cstdlib>
#include <bitset>
#include <algorithm>
#include <random>
#include <algorithm>
#include <optional>
#include <set>

using namespace std;
//typedef long long int ll;

#define cin1(a) cin >> a;
#define cin2(a, b) cin >> a >> b;
#define cin3(a, b, c) cin >> a >> b >> c;

#define cinvec(vec) for(auto& it: vec) {cin1(it);}
#define coutvec(vec) for(auto it: vec) {cout << it << " ";} cout << endl;
//#define endl '\n';

using namespace std;

#define int int64_t

bool isPrime(int x) {
    if (x <= 1) return true;
    for (int i = 2; (i * i) <= x; i++) {
        if (x % i == 0) {
            return false;
        }
    }
    return true;
}

int primesCnt(int x) {
    int res = 0;
    if (x <= 1) return res;
    int x_b = x;
    for (int i = 2; (i * i) <= x_b; i++) {
        while (x % i == 0) {
            x /= i;
            res++;
        }
    }
    if (x != 1) res++;
    return res;
}

set<int> difPrimes(int x) {
    set<int> res;
    if (x <= 1) return res;
    int x_b = x;
    for (int i = 2; (i * i) <= x_b; i++) {
        while (x % i == 0) {
            x /= i;
            res.insert(i);
        }
    }
    if (x != 1) res.insert(x);
    return res;
}

string solve() {
    int x, d;
    cin2(x, d);

    int degsCnt = 0;

    while (x % d == 0) {
        x /= d;
        degsCnt++;
    }

    if (degsCnt == 1) return "No";
    
    if (!isPrime(x)) return "Yes";

    if (isPrime(d)) return "No";

    if (degsCnt == 2) return "No";

    int dPrimesCnt = primesCnt(d);
    
    if (x == 1) {
        return "Yes";
    }
    else {
        set<int> dif = difPrimes(d);

        if (degsCnt == 3) {
            if (dif.size() == 1) {
                if ((dPrimesCnt <= 2) && dif.count(x)) return "No";
                else return "Yes";
            }
            else {
                return "Yes";
            }
        }
        else { // degsCnt > 3
            return "Yes";
        }
    }
}

int32_t main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    cin1(t);
    //t = 1;

    while (t--) {
        //solve();
        cout << solve() << endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array